package com.winning.pmph.ctrl;

import com.winning.pmph.entity.Book;
import com.winning.pmph.service.BookService;
import com.winning.pmph.utils.Const;
import com.winning.pmph.utils.PMPHAppUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "知识工具管理/图书标注")
@RestController
@RequestMapping("/pmph/action/mark")
public class MarkCtrl {
    @Resource
    private BookService bookService;

    /**
     * 获取当前用户正在标记的图书
     */
    @ApiOperation(value = "selectMarkedBookList", notes = "获取当前用户正在标记的图书")
    @GetMapping("selectMarkedBookList")
    public List<Book> selectMarkedBookList() {
        return bookService.lambdaQuery()
                .select(Book::getId, Book::getName, Book::getIsbn, Book::getMarkedRate, Book::getUpdatedTime)
                .eq(Book::getStatus, Book.BOOK_STATUS_PUBLISHED)
                .eq(Book::getMarkedStatus, Const.BOOK_MARKED_STATUS_MARKING)
                .eq(Book::getMarkedBy, PMPHAppUtil.getCurrentUserEnName())
                .orderByDesc(Book::getUpdatedTime)
                .orderByAsc(Book::getId)
                .list();
    }

    /**
     * 获取未标记的图书
     */
    @ApiOperation(value = "selectUnMarkedBookList", notes = "获取未标记的图书")
    @GetMapping("selectUnMarkedBookList")
    public List<Book> selectUnMarkedBookList() {
        return bookService.lambdaQuery()
                .select(Book::getId, Book::getName, Book::getIsbn, Book::getUpdatedTime)
                .eq(Book::getStatus, Book.BOOK_STATUS_PUBLISHED)
                .eq(Book::getMarkedStatus, Const.BOOK_MARKED_STATUS_UNMARKED)
                .orderByDesc(Book::getUpdatedTime)
                .orderByAsc(Book::getId)
                .list();
    }

    /**
     * 修改图书标注标记
     */
    @ApiOperation(value = "insertMarkedBookList", notes = "修改图书标注标记")
    @PostMapping("insertMarkedBookList")
    public void insertMarkedBookList(@RequestBody Book book) {
        bookService.lambdaUpdate()
                .eq(Book::getId, book.getId())
                .set(Book::getMarkedStatus, Const.BOOK_MARKED_STATUS_MARKING)
                .set(Book::getMarkedBy, PMPHAppUtil.getCurrentUserEnName()).update(book);
    }
}
